package net.viralpatel.struts2;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.util.Map;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class ChangeProfileAction extends ActionSupport {
	private String username;
	private String name;
	private String email;
	private String tel;
	private String com;
	String url = "jdbc:mysql://localhost:3306/";
	String dbName = "t10";
	String driverName = "com.mysql.jdbc.Driver";
	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getTel() {
		return tel;
	}

	public void setTel(String tel) {
		this.tel = tel;
	}

	public String getCom() {
		return com;
	}

	public void setCom(String com) {
		this.com = com;
	}

	Connection con = null;

	String nameroot = "root";
	String passwordroot = "";

	public String execute() throws Exception {
		
		Map session = ActionContext.getContext().getSession();
		String username= session.get("username").toString();
		try {
			Class.forName(driverName);
			con = DriverManager.getConnection(url + dbName, nameroot,
					passwordroot);
		} catch (ClassNotFoundException e) {
			System.out.println(e.getMessage());
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}

		String selectChangePasswd = "Update user set userID =?,fullname = ?,mail=?," +
				"tel=?,company=? where userID =?";
		PreparedStatement prepStmt = con.prepareStatement(selectChangePasswd);
		prepStmt.setString(1, username);
		prepStmt.setString(2, this.name);
		prepStmt.setString(3, this.email);
		prepStmt.setString(4, this.tel);
		prepStmt.setString(5, this.com);
		prepStmt.setString(6, session.get("username").toString());
		int row = prepStmt.executeUpdate();
		if (row != 0) {
			session.put("username", this.username);
			return SUCCESS;
		} else {
			addActionError(getText("error.changeprofile"));
			return ERROR;
		}
	}


}
